<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\common\manager;

use app\common\model\Team;
use app\common\model\User;
use app\common\model\UserTeam;
use app\common\model\WriteOff;
use think\Db;
use think\Exception;

class LogManager extends BaseManager
{
    CONST TYPE_CLIENT = 'Client[客户端]';
    CONST TYPE_SERVER = 'Server[后台]';
    const MATCH_APPLY_REDIRECT_PATH = LOG_PATH.'match_apply/';
    const MATCH_REDIRECT_PATH = LOG_PATH.'match/';
    public static function Log($username,$userId,$tips,array $data,string $file,$startTime,$endTime,string $type=self::TYPE_CLIENT,$redirect=self::MATCH_REDIRECT_PATH)
    {
        try {
            //file_put_contents('log.txt',$redirect,FILE_APPEND);
            if(!is_dir($redirect)){
                if (!mkdir($redirect,0755,true)){
                    throw new Exception('当前目录不存在，目录创建失败');
                }
            }
            $jsonData['标签:'] = $tips;
            $jsonData['类型:'] = $type;
            $jsonData['执行人:'] = $username;
            $jsonData['执行人Id:'] = $userId;
            $jsonData['开始时间:'] = $startTime;
            $jsonData['结束时间:'] = $endTime;
            $jsonData['数据:'] = $data;
            file_put_contents($redirect.'/'.$file,json_encode($jsonData,JSON_UNESCAPED_UNICODE).PHP_EOL,FILE_APPEND);
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $e){
            self::setStatusCode(self::$failCode);
            self::setMessage($e->getMessage());
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }
}